// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

/* Vue全家桶 */
import Vue from 'vue'
Vue.config.productionTip = false;

import "babel-polyfill";
import './directives.js'; // 引入自定义指令

import 'element-ui/lib/theme-chalk/index.css'; //饿了么UI
import ElementUI from 'element-ui';
Vue.use(ElementUI);

import 'font-awesome/css/font-awesome.css'; //cnpm install font-awesome

import echarts from 'echarts'; //echarts图表
Vue.prototype.$echarts = echarts;
// Vue.use(layUI);
import Print from 'vue-print-nb';
Vue.use(Print)

import "../static/hzcss.css"


const globalVar = { // 全局 每个页面共有的变量

    globalTemp: {},

    globalArray: [],
    globalObject: {},
    globalString: '',
    globalNumber: 0,
    globalBoolean: false,

    activeDate: 0,
};

const $0 = $;
const $msg = function(_this,msg,type){
    return _this.$message({
        message: msg,
        type,
        showClose: true,
        duration: 2000
    });
};
$0.with({
    msgError: msg=>$msg($1[0],msg,'error'),
    msgSuccess: msg=>$msg($1[0],msg,'success'),
    msgWarning: msg=>$msg($1[0],msg,'warning'),
    msgInfo: msg=>$msg($1[0],msg,'info'),
    loading(text,fn) {
        text = $1[0].$loading({
            lock: true,
            text: text,
            // spinner: icon||'',
            background: 'rgba(0, 0, 0, 0.5)'
        });
        typeof fn === 'function' && setTimeout( ()=>{
            fn(text) !== true && text.close();
        }, ( fn.name.replace('sleep','') * 1 ) || 1500 )
        return text;
    },
});
const { $1, $all, Window } = $0.bindVue(
    Vue,

    { //全局 挂载Vue的 bindVue选项:: globalVars、 globalMethods ••••
        globalVars(){
            return {
                globalVar: globalVar,
                Height: Window.innerHeight,
                Width: Window.innerWidth,

                Temp: {},
                This: {},

                array: [],
                array2: [],
                object: {},
                object2: {},
                string: '',
                string2: '',
                number: 0,
                number2: 0,
                boolean: false,
                boolean2: true,
            };
        },
        globalMethods: { //全局 每个页面共有的方法
            setIndex(index) {
                // console.error(this.currentPage,this.pagerSize);
                return ( (this.currentPage||1) - 1)*this.pagerSize + index + 1;
            },
            checkPhone(id,test,tip) {
                id = $(id);
                if(!id.length) return;
                if(test==='') return;
                if( !/^1(3|4|5|7|8)\d{9}$/.test(test) && !/^0\d{2,3}-?\d{7,8}$/.test(test) ) return myTip.call( id, tip, 'warning', 170, 46, 'up' ); //.css('left','35px');
            },
        }
    },

    { //以下为Vue原型扩展方法
        Return: Return,
        Case: Case,
        $echarts: echarts,
        getThis: function(){ return this; },
        console: function(a){
            console.warn.apply(console,arguments);
            return this;
        },
        parseDadSon: function(name,call){
            const dadSon = this['dadSon'];
            var dad, son;
            if( dadSon ){
                dad = this.parent = dadSon[0];
                dad[son=dadSon[1]] = this;
            }else {
                dad = this.dad;
                if(dad) {
                    this.parent = dad;
                }else {
                    dad = this.parent = this.$parent;
                };

                son = this.son;
                son ? dad[son] = this : dad.hasOwnProperty(name) ? dad[name] = myConcat( dad[name], this ) : dad[name] = this;
            };
            call && call(son);
            return dad;
        }
    }
);

const list = $0.resize.list;
Window.addEventListener('resize', function(e){
    const W = Window.innerWidth;
    const H = Window.innerHeight;
    var $ = $1[0];
    if( $ ) { $.Width = W; $.Height = H; $all.forEach( v=>typeof v.onResize==='function' && v.onResize(W,H,e,Window) ); }
    for( $ in list ) { list[$](W,H,e,Window); }
},false);

Window.addEventListener('scroll', function(e){
    $all.forEach( v=>typeof v.onScroll==='function' && v.onScroll(e,Window) );
},false);

import '@/myComponents/install.js';

/* eslint-disable no-new */
import App from './App'
import router from './router'
// import store from './store'
import myLogin from '@/views/login.vue';
new Vue({
    el: '#app',
    data: {
        isLogined: localStorage.getItem('userId') !== null,
        join() { Window.$1 = $1[0] = myLogin.render.this },
        exit(bool){
            bool !== true
            ? $.loading('正在退出..',()=>{
                this.isLogined = false;
            })
            : this.isLogined = false;
            G.clearLocal(["roleId","roleName","loginName","userId","selectedMenu","menus","userPassword"]);
        }
    },
    created() {
        const path = this.$route.path;
        ( !G.local('userId') || path === '/' || path === '/login' ) && ( router.replace({ name: "login" }), location.hash = '#/login' );
        G.app = this;
    },
    // mounted() {  },
    watch: {
        isLogined: {
            immediate: true,
            handler(bool){
                bool === false && ( router.replace({ name: "login" }), location.hash = '#/login' );
            }
        }
    },
    router,
    components: { myLogin, App },
    template: `
        <div style=height:100% >
            <App v-if="isLogined" />
            <myLogin v-else @hook:mounted='join' />
        </div>
    `
});

// console.error(router);

export default {
    props: {
        native: Boolean,
        wrapStyle: {},
        wrapClass: {},
        viewClass: {},
        viewStyle: {},
        noresize: Boolean, // 如果 container 尺寸不会发生变化，最好设置它可以优化性能
        tag: {
            type: String,
            default: 'div'
        }
    },
  }
